package com.example.demo.service;

import com.example.demo.config.UserResourceProperties;
import com.example.demo.dao.CustomMapper;
import com.example.demo.dao.UserMapper;
import com.example.demo.entity.User;
import com.example.demo.pojo.BSResult;
import com.example.demo.utils.BSResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements  UserService {

@Autowired
    private UserMapper userMapper;

@Autowired
   private UserResourceProperties userResourceProperties;

@Autowired
    private CustomMapper customMapper;

    @Override
    @Transactional
    public BSResult save(User user) {

        user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
       user.setActive(userResourceProperties.getActive());
       user.setDetailAddress(userResourceProperties.getDetailAddress());
       user.setLocation(userResourceProperties.getLocation());
       user.setCreated(new Date());
       user.setUpdated(new Date());
       user.setImageUrl("upload/usericon/original.jpg");
        userMapper.insert(user);


        return BSResultUtils.success(user);
    }


    @Override
    public BSResult checkUserByUsername(String username) {

            Example example=new Example(User.class);
            Example.Criteria exampleC=example.createCriteria();
            exampleC.andEqualTo("username",username);
            List<User> userList=userMapper.selectByExample(example);
            if(userList.size()==0 ||userList==null)
                return BSResultUtils.build(200,"用户名可以使用",true);
            else
                return BSResultUtils.build(400,"用户名已经存在",false);

        }



    @Override
    @Transactional
    public BSResult updateUser(User user) {

        try{
           user.setUpdated(new Date());
           if("".equals(user.getPassword()))
               user.setPassword(null);
           userMapper.updateByPrimaryKeySelective(user);

           return BSResultUtils.build(200,"OK",true);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }

    }

    @Override
    public BSResult compareAndChangePwd(int id,String oldPassword,String newPassword) {


        User user=userMapper.selectByPrimaryKey(id);
        String oldpwd=DigestUtils.md5DigestAsHex(oldPassword.getBytes());
        String newpwd=DigestUtils.md5DigestAsHex(newPassword.getBytes());

        if(!oldpwd.equals(user.getPassword()))

            return BSResultUtils.build(400,"旧密码不正确",false);

        user.setPassword(newpwd);

        try{
            userMapper.updateByPrimaryKeySelective(user);
            return BSResultUtils.build(200,"修改密码成功",true);
        }catch(Exception e){
            e.printStackTrace();
            return BSResultUtils.build(400,"修改密码失败",false);
        }
    }

    @Override
    public User findUserById(int id) {

        User user=userMapper.selectByPrimaryKey(id);
        return user;


    }

    @Override
    public List<User> findAllUsers() {
        return userMapper.selectAll();
    }

    @Override
    public User addUser(User user){
        user.setActive("1");
        String pwd=user.getPassword();
        String newpwd=DigestUtils.md5DigestAsHex(pwd.getBytes());
        user.setUpdated(new Date());
        user.setCreated(new Date());
        userMapper.insert(user);
        return user;
    }

    @Override
    public void deleteById(Integer id) {
        userMapper.deleteByPrimaryKey(id);

    }

    @Override
    public List<User> findUserByRoleId(int roleId) {
        return customMapper.findUserByrRoleId(roleId);
    }
}

